<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="##Android AIDL学习（一）  在测试机的/mnt/sdcard/路径下放一首mp3格式的音乐文件，故音乐文件的路径为/mnt/sdcard/music.mp3 在Android Studio中创建一个空的Android项目，在项目下新建client与server两个module。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-AIDL学习（一）">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android/Android-AIDL学习（一）/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="##Android AIDL学习（一）  在测试机的/mnt/sdcard/路径下放一首mp3格式的音乐文件，故音乐文件的路径为/mnt/sdcard/music.mp3 在Android Studio中创建一个空的Android项目，在项目下新建client与server两个module。">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-4aea40a06db23dcd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:19:34.925Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-AIDL学习（一）">
<meta name="twitter:description" content="##Android AIDL学习（一）  在测试机的/mnt/sdcard/路径下放一首mp3格式的音乐文件，故音乐文件的路径为/mnt/sdcard/music.mp3 在Android Studio中创建一个空的Android项目，在项目下新建client与server两个module。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/3110861-4aea40a06db23dcd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android/Android-AIDL学习（一）/"/>





  <title>Android-AIDL学习（一） | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android/Android-AIDL学习（一）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-AIDL学习（一）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>##Android AIDL学习（一）</p>
<ol>
<li>在测试机的/mnt/sdcard/路径下放一首mp3格式的音乐文件，故音乐文件的路径为/mnt/sdcard/music.mp3</li>
<li>在Android Studio中创建一个空的Android项目，在项目下新建client与server两个module。</li>
</ol>
<a id="more"></a>
<ol start="3">
<li>首先操作server这个module，先创建一个AIDL文件夹，在Studio中可以直接选择AIDL文件夹新建，因此不用考虑文件的路径，直接选择默认的就好，接下来在AIDL文件夹下新建IPlayServiceAIDL.aidl文件，直接选择AIDL文件新建，则可以省去自己写文件中的部分代码。接下来在添加两个方法，一个为play(),一个为stop(),切勿使用private,protected,public来修饰，但必须要写返回值。然后build-&gt;make project编译工程。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">package com.mazaiting.server;</span><br><span class="line"></span><br><span class="line">interface IPlayServiceAIDL &#123;</span><br><span class="line"></span><br><span class="line">    void play();</span><br><span class="line"></span><br><span class="line">    void stop();</span><br><span class="line"></span><br><span class="line">    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,</span><br><span class="line">            double aDouble, String aString);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>编译成功后，如下图路径中所示，会生成一个IPlayServiceAIDL文件。</p>
<ol start="4">
<li>接下来在java的包下创建一个PlayService，创建时最好创建一个Service文件，这样可以少写一些代码。由于本文主要写AIDL的使用，故主要说明AIDL的使用流程，至于如何播放音频，后续再进行讲解。接着在  PlayService中的onBind方法中返回一个IBinder对象,这个IBinder对象是由IPlayServiceAIDL.Stub 创建出来的，IBinder对象创建方法如下:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">private IBinder mBinder = new IPlayServiceAIDL.Stub()&#123;</span><br><span class="line"></span><br><span class="line">    @Override public void play() throws RemoteException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void stop() throws RemoteException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble,</span><br><span class="line">        String aString) throws RemoteException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>在onBinder方法中将mBinder返回就好。PlayService完整代码如下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">public class PlayService extends Service &#123;</span><br><span class="line">  private MediaPlayer mediaPlayer;</span><br><span class="line">  public PlayService() &#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override public IBinder onBind(Intent intent) &#123;</span><br><span class="line">    try&#123;</span><br><span class="line">      mediaPlayer = new MediaPlayer();</span><br><span class="line">      String path = Environment.getExternalStorageDirectory()+&quot;/music.mp3&quot;;</span><br><span class="line">      File file = new File(path);</span><br><span class="line">      FileInputStream fis = new FileInputStream(file);</span><br><span class="line">      mediaPlayer.setDataSource(fis.getFD());</span><br><span class="line">      mediaPlayer.setLooping(true);</span><br><span class="line">    &#125;catch (Exception e)&#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    return mBinder;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private IBinder mBinder = new IPlayServiceAIDL.Stub()&#123;</span><br><span class="line"></span><br><span class="line">    @Override public void play() throws RemoteException &#123;</span><br><span class="line">      if (mediaPlayer.isPlaying())&#123;</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line">      mediaPlayer.prepareAsync();</span><br><span class="line">      mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() &#123;</span><br><span class="line">        @Override public void onPrepared(MediaPlayer mp) &#123;</span><br><span class="line">          mediaPlayer.start();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void stop() throws RemoteException &#123;</span><br><span class="line">      if (mediaPlayer.isPlaying())&#123;</span><br><span class="line">        mediaPlayer.stop();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble,</span><br><span class="line">        String aString) throws RemoteException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ol start="5">
<li>打开server这个module中的AndroidManifest.xml，并配置清单文件。<br>首先需要配置访问内存卡权限android.permission.READ_EXTERNAL_STORAGE，其次需要在service节点中配置android:process=”:remote” 属性，表示运行在单独的进程，并在service节点下intent-filter中配置action为com.mazaiting.server.PlayService。配置清单如下：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;</span><br><span class="line">  &lt;application</span><br><span class="line">      android:allowBackup=&quot;true&quot;</span><br><span class="line">      android:icon=&quot;@mipmap/ic_launcher&quot;</span><br><span class="line">      android:label=&quot;@string/app_name&quot;</span><br><span class="line">      android:supportsRtl=&quot;true&quot;</span><br><span class="line">      android:theme=&quot;@style/AppTheme&quot;&gt;</span><br><span class="line">    &lt;service</span><br><span class="line">        android:name=&quot;.PlayService&quot;</span><br><span class="line">        android:process=&quot;:remote&quot;</span><br><span class="line">        android:enabled=&quot;true&quot;</span><br><span class="line">        android:exported=&quot;true&quot;&gt;</span><br><span class="line">        &lt;intent-filter&gt;</span><br><span class="line">               &lt;action android:name=&quot;com.mazaiting.server.PlayService&quot;/&gt;</span><br><span class="line">              &lt;/intent-filter&gt;</span><br><span class="line">      &lt;/service&gt;</span><br><span class="line">  &lt;/application&gt;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>6.接下来操作client这个module，在client中创建AIDL文件夹，将server中的IPlayServiceAIDL.adil文件拷贝到client的AIDL文件夹中，拷贝时注意文件的包名，路径一定要相同。如图:</p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-4aea40a06db23dcd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="目录结构"></p>
<p>注意观察两个aidl文件夹下的包名及文件，是完全相同的。</p>
<p>7.在java中创建MainActivity.java,并创建activity_main.xml布局文件，其中布局文件内容为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_main&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:orientation=&quot;horizontal&quot;</span><br><span class="line">    tools:context=&quot;com.mazaiting.aidldemo1.MainActivity&quot;</span><br><span class="line">    &gt;</span><br><span class="line"></span><br><span class="line">  &lt;Button</span><br><span class="line">      android:id=&quot;@+id/btn_start&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:text=&quot;start&quot;</span><br><span class="line">      /&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  &lt;Button</span><br><span class="line">      android:id=&quot;@+id/btn_stop&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:text=&quot;stop&quot;</span><br><span class="line">      /&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>接下来编写Activity中的代码，远程调用时我们需要调用boolean bindService(Intent service, ServiceConnection conn,<br>            int flags)方法来进行绑定，其中Intent 为其设置PlayService的action，ServiceConncetion的创建代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">private ServiceConnection mConncet = new ServiceConnection() &#123;</span><br><span class="line">    @Override public void onServiceConnected(ComponentName name, IBinder service) &#123;</span><br><span class="line">      mBinder = IPlayServiceAIDL.Stub.asInterface(service);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void onServiceDisconnected(ComponentName name) &#123;</span><br><span class="line">      mBinder = null;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure></p>
<p>mBinder是一个IPlayServiceAIDL对象，在onResume中调用bindService，在onPause中调用unbindService来解除绑定。<br>MainActivity中的全部代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">public class MainActivity extends AppCompatActivity implements View.OnClickListener &#123;</span><br><span class="line">  private static final String ACTION = &quot;com.mazaiting.server.PlayService&quot;;</span><br><span class="line">  private Button btnStart,btnStop;</span><br><span class="line">  private IPlayServiceAIDL mBinder;</span><br><span class="line">  private ServiceConnection mConncet = new ServiceConnection() &#123;</span><br><span class="line">    @Override public void onServiceConnected(ComponentName name, IBinder service) &#123;</span><br><span class="line">      mBinder = IPlayServiceAIDL.Stub.asInterface(service);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void onServiceDisconnected(ComponentName name) &#123;</span><br><span class="line">      mBinder = null;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  @Override protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line">    setContentView(R.layout.activity_main);</span><br><span class="line">    initView();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override protected void onResume() &#123;</span><br><span class="line">    super.onResume();</span><br><span class="line">    Intent service = new Intent(ACTION);</span><br><span class="line">    bindService(service,mConncet, Context.BIND_AUTO_CREATE);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override protected void onPause() &#123;</span><br><span class="line">    super.onPause();</span><br><span class="line">      unbindService(mConncet);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private void initView() &#123;</span><br><span class="line">    btnStart = (Button) findViewById(R.id.btn_start);</span><br><span class="line">    btnStop = (Button) findViewById(R.id.btn_stop);</span><br><span class="line">    btnStart.setOnClickListener(this);</span><br><span class="line">    btnStop.setOnClickListener(this);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override public void onClick(View v) &#123;</span><br><span class="line">    switch (v.getId())&#123;</span><br><span class="line">      case R.id.btn_start:</span><br><span class="line">        try &#123;</span><br><span class="line">          mBinder.play();</span><br><span class="line">        &#125; catch (RemoteException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        break;</span><br><span class="line">      case R.id.btn_stop:</span><br><span class="line">        try &#123;</span><br><span class="line">          mBinder.stop();</span><br><span class="line">        &#125; catch (RemoteException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        break;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a href="http://git.oschina.net/zaitingma/codes/1hzylv26mjb9i7kcwet4r25" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-AccessibilityService无障碍服务（二）/" rel="next" title="Android-AccessibilityService无障碍服务（二）">
                <i class="fa fa-chevron-left"></i> Android-AccessibilityService无障碍服务（二）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-AIDL学习（二）/" rel="prev" title="Android-AIDL学习（二）">
                Android-AIDL学习（二） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
